package edu.hyh.controllers;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import edu.hyh.dao.HyhDao;
import edu.hyh.models.Hyh;
//import edu.hziee.models.Book;
@Controller
@RequestMapping("/hyh")
public class HyhController {
	
	@Autowired
	private HyhDao hyhDao;


	@RequestMapping(method=RequestMethod.GET)
	public ModelAndView getAll(){
		ModelAndView mav=new ModelAndView("everybody");
		List<Hyh> hyhs=hyhDao.findAll();
		mav.addObject("hyhs",hyhs);
		return mav;
	}

	@RequestMapping(value="/{id}",method=RequestMethod.GET)
	public ModelAndView get(@PathVariable int id){
		ModelAndView mav=new ModelAndView("hyh");
		Hyh hyh=hyhDao.findById(id);
		mav.addObject("hyh",hyh);
		return mav;
	}
	@RequestMapping(value="/add",method=RequestMethod.GET)
	public ModelAndView add(){
		ModelAndView mav=new ModelAndView("addfood");
				return mav;
	}
	
	@RequestMapping(value="/add",method=RequestMethod.POST)
	public ModelAndView add(@RequestParam String title,
			@RequestParam String img
			){
		Hyh hyh=new Hyh();
		hyh.setTitle(title);
		hyh.setImg(img);
		hyhDao.add(hyh);
		
		ModelAndView mav=new ModelAndView("redirect:/hyh");
		return mav;
		
	}
	@RequestMapping(value="/delete",method=RequestMethod.GET)
	public ModelAndView delete(@RequestParam int id){		
		hyhDao.delete(id);
		ModelAndView mav=new ModelAndView("redirect:/hyh");
		return mav;
		
}
	@RequestMapping(value="/edit",method=RequestMethod.GET)
	public ModelAndView edit(@RequestParam int id){
		Hyh hyh=hyhDao.findById(id);
		ModelAndView mav=new ModelAndView("edit");
		mav.addObject("hyh",hyh);
		return mav;
	}
	@RequestMapping(value="/edit",method=RequestMethod.POST)
	public ModelAndView edit(@RequestParam int id,
			@RequestParam String title,
			@RequestParam String img
			){
		
		Hyh hyh =new Hyh();
		hyh.setId(id);
		hyh.setImg(img);
		hyh.setTitle(title);
		
		hyhDao.modify(hyh);
		
		ModelAndView mav=new ModelAndView("redirect:/hyh");
		return mav;
		
	}
	
	@RequestMapping(value="/xq3",method=RequestMethod.GET)
	public ModelAndView tiaoshi(){
		
		ModelAndView mav = new ModelAndView("xq3");
		return mav;
	}
}
